package com.wyu.dao;

import com.alibaba.druid.util.JdbcUtils;
import com.wyu.pojo.YG;
import com.wyu.util.GetCt;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class YGDaoImpl implements YGDao {
    @Override
    public YG selectByYgh(String ygh) {
        //查询数据库：JdbcUtil工具类去查询
        //创建一个操作数据库对象
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        YG yg = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "select * from ygb where ygh=?";
            BeanHandler<YG> handler = new BeanHandler(YG.class);
            yg = qr.query(ct,sql,handler,ygh);
            System.out.println(yg);

        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return yg;
    }

    @Override
    public int insertYG(String ygh, String xm, String mm3, String xb, String zw, String bz) {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "insert into ygb (ygh,xm,mm,xb,zw,bz) values (?,?,?,?,?,?);";
            int i = qr.update(ct,sql,ygh,xm,mm3,xb,zw,bz);
            if (i>0){
                System.out.println("成功添加"+i+"条数据");
                return i;
            }
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return 0;
    }


    @Override
    public List<YG> searchyg() {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "select * from ygb where bz!=0;";
            List<String> list1 = new ArrayList<>();
            BeanListHandler<YG> handler = new BeanListHandler(YG.class);
            List<YG> list = qr.query(ct,sql,handler,list1.toArray());
            System.out.println(list);
            return list;
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return null;
    }

    @Override
    public int deleteYgByGh(String ygh) {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "delete from ygb where ygh = ?;";
            int i = qr.update(ct,sql,ygh);
            if (i>0){
                System.out.println("删除成功"+i+"条数据");
            }
            return i;
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return 0;
    }

    @Override
    public int updatePassword(int yuangonghao, String newmm) {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        try {
            ct =  GetCt.getConnection();
            String sql = "update ygb set mm=? where ygh=?";
            int i =  qr.update(ct,sql,newmm,yuangonghao);
            if(i>0){
                System.out.println("修改成功"+i+"条数据");
                return 1;
            }
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return 0;
    }


}
